package com.wx.mythread.threadmethod;

/**
 * 3个线程，线程1和线程2阻塞，线程3唤醒他们，用notify和notifyAll
 * 线程先调用start方法，并不代表先执行
 */
public class StudyWaitNotifyAll implements Runnable{

    private static final Object resourcesA = new Object();

    @Override
    public void run() {
        synchronized (resourcesA){
            System.out.println(Thread.currentThread().getName()+"got resourcesA lock.");
            try {
                System.out.println(Thread.currentThread().getName() + "waits to start.");
                resourcesA.wait();
                System.out.println(Thread.currentThread().getName() + "'s wait to end");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        StudyWaitNotifyAll r = new StudyWaitNotifyAll();
        Thread threadA = new Thread(r);
        Thread threadB = new Thread(r);
        Thread threadC = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (resourcesA) {
                    System.out.println(Thread.currentThread().getName() + "start notifyall");
                    resourcesA.notifyAll();
                    // resourcesA.notify();
                    System.out.println(Thread.currentThread().getName() + "end");
                }
            }
        });
        threadA.start();
        threadB.start();
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        threadC.start();
    }
}
